package com.itheima.config;


import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;


public class MybatisConfig {
    //    <!--2. 配置SqlSessionFactoryBean-->
//    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    @Bean
    //这里的参数由spring容器给; 如果spring容器中有就会传入, 没有这个bean对象就会报错
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //        <property name="dataSource" ref="dataSource"/>
        sqlSessionFactoryBean.setDataSource(dataSource);

        // 配置别名       <property name="typeAliasesPackage" value="com.itheima.bean"/>
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.bean");

        return sqlSessionFactoryBean;
    }
    //    </bean>



//    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        //        <property name="basePackage" value="com.itheima.mapper"/>
        mapperScannerConfigurer.setBasePackage("com.itheima.mapper");
        return mapperScannerConfigurer;
    }
//    </bean>
}
